﻿using MySql.Data.MySqlClient;
using sqlsearcher.Services.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace sqlsearcher.Services.DB
{
    /// <summary>
    /// MySQLHelper
    /// 创建日期 2023/1/17 16:00:27
    /// Author: 名字什么都是浮云
    /// </summary>	
    public class MySQLHelper : ISqlHelper
    {
        private string server;
        private string uid;
        private string pwd;
        private int dbType;
        private string dbName;
        public static string SERVER = string.Empty;
        public static string CONNSTR = string.Empty;
        private static string ConnectionString = string.Empty;
        public MySQLHelper(string server, string uid, string pwd, int dbType)
        {
            this.server = server;
            this.uid = uid;
            this.pwd = pwd;
            this.dbType = dbType;
        }
        public MySQLHelper(string server, string uid, string pwd, string dbName)
        {
            this.server = server;
            this.dbName = dbName;
            this.server = server;
            this.uid = uid;
            this.pwd = pwd;
        }

        
       
        public string GetConnStr()
        {
            var address = "";
            var port = 3306;
            if (this.server.LastIndexOf(":") != -1)
            {
                address = this.server.Split(':')[0];
                port = this.server.Split(':')[1].TryInt(0);
            }
            else
            {
                address = this.server;
            }

            if (string.IsNullOrEmpty(this.server) || string.IsNullOrEmpty(this.uid) || string.IsNullOrEmpty(this.pwd))
            {
                throw new Exception("连接失败!");
            }

            if (string.IsNullOrEmpty(this.dbName))
            {
                CONNSTR = $"server={address};port={port};user={this.uid};password={this.pwd};";
            }
            else
            {
                CONNSTR = $"server={address};port={port};user={this.uid};password={this.pwd};database={this.dbName}";
            }
           
            ConnectionString = $"server={address};port={port};user={this.uid};password={this.pwd};";
            SERVER = server;
            return CONNSTR;

            
        }

        public static string BuilderConnectionString(string selectdb)
        {
            return $"{ConnectionString};database={selectdb}";
        }

        public bool TryConn()
        {
            MySqlConnection conn = new MySqlConnection(GetConnStr());
            try
            {
                conn.Open();//打开通道，建立连接，可能出现异常，使用try catch语句
                return true;
            }
            catch (MySqlException ex)
            {
            }
            finally
            {
                conn.Close();
            }
            return false;
        }

        public string GetDBName()
        {
            return server;
        }
    }
}
